package com.buyi.easyhome

import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.buyi.easyhome.receiver.BatteryReceiver
import com.buyi.easyhome.receiver.ScreenBroadcastReceiver
import com.buyi.easyhome.ui.theme.EasyHomeTheme
import com.buyi.easyhome.ui.view.EhApp
import com.buyi.easyhome.util.TTSHelper
import com.buyi.easyhome.util.VersionCheckerUtil


class MainActivity : ComponentActivity() {

    private var batteryReceiver: BatteryReceiver? = null
    private var screenBroadcastReceiver: ScreenBroadcastReceiver? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            EasyHomeTheme {
                EhApp(
                    checkVersionAction = {
                        checkVersion()
                    },
                )
            }
        }

//        TTSHelper.getInstance( this)

        // 初始化电池广播接收器
        batteryReceiver = BatteryReceiver()
        // 注册广播接收器，监听电池状态变化
        val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED)
        registerReceiver(batteryReceiver, intentFilter)


        // 初始化屏幕广播接收器
        screenBroadcastReceiver = ScreenBroadcastReceiver()
        // 注册广播接收器，监听屏幕状态变化
        registerReceiver(screenBroadcastReceiver, IntentFilter(Intent.ACTION_SCREEN_ON))
    }

    override fun onDestroy() {
        super.onDestroy()
        // 注销广播接收器
        if (batteryReceiver != null) {
            unregisterReceiver(batteryReceiver);
        }
        if (screenBroadcastReceiver != null) {
            unregisterReceiver(screenBroadcastReceiver);
        }
        // 关闭TTS
        TTSHelper.destroy()
    }

    private fun checkVersion() {
        val requestUrl =
            "https://gitee.com/api/v5/repos/buyili/easy-home/releases?page=1&per_page=2&direction=desc"
        val currentVersion = getCurrentAppVersion()

        VersionCheckerUtil.checkForUpdate(
            this,
            requestUrl,
            currentVersion,
            object : VersionCheckerUtil.VersionCheckCallback {
                override fun onNeedUpdate(downloadUrl: String) {
                    // 显示更新弹窗或执行下载逻辑
                    Log.d("Update", "发现新版本，下载地址：$downloadUrl")
                }

                override fun onNoUpdateNeeded() {
                    Log.d("Update", "已经是最新版本")
                    Toast.makeText(this@MainActivity, "已经是最新版本", Toast.LENGTH_SHORT).show()
                }

                override fun onError(errorMsg: String?) {
                    Log.e("Update", "检查更新失败: $errorMsg")
                    Toast.makeText(this@MainActivity, "检查更新失败", Toast.LENGTH_SHORT).show()
                }
            }
        )
    }

    private fun getCurrentAppVersion(): String {
        return try {
            val pInfo = packageManager.getPackageInfo(packageName, 0)
            pInfo.versionName
        } catch (e: Exception) {
            "0.0.0"
        }.toString()
    }

}
